home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Magnum One
/
Magnum One (Mid-American Digital) (Disc Manufacturing).iso
/
d20
/
filelist.arc
/
FILELIST.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-04-17
|
12KB
|
449 lines
/* Filelist.c -- Public Domain -- By Erik Vanriper -- SoWhutWare 1991
**
** Why? Because I had nothing better to do.
**
** I probably could have done this 10 other ways, but I figured this works,
** so why break it. The only thing left to add is the wildcard expansion
** on files. Maybe tomorrow.....
**
** I used Turbo C 2.01 on this, hack it to your hearts desire.
**
** If you use a function, please give credit, I know I did... Thanx Bob Stout!
**
** gotoxy(over, down); (Just a reminder for myself...)
**
** If the code format looks wierd, I used a tab stop of 3, and QEdit.
**
** I also used TheDraw 4.01 to create the initial screen, FILELIST.ANS, and
** saved it under C, and Normal, everything else is default, filename is
** FILELIST.H.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <alloc.h>
#include <dir.h>
#include <sys\types.h>
#include <sys\stat.h>
#include <time.h>
#include <process.h>
#include <graphics.h>
#include "filelist.h"
#define NUL '\0' /* This is part of Bob Stouts "commafmt()" code */
struct xx
{
char FilePath[80]; /* 80 chars for the path and filename, */
char AreaDesc[200]; /* 200 chars for the description. */
};
struct xx *path[80]; /* Max of 80 areas to malloc() 22,400 bytes. */
struct stat info;
struct tm *tmfile;
struct ffblk fileinfo;
char drive[MAXDRIVE];
char dir[MAXDIR];
char filename[MAXFILE];
char ext[MAXEXT];
char filepath[MAXPATH];
int tempcount = 0;
int start = 0;
int numfiles[80];
long numbytes, areabytes;
char descrip[200], keeppath[200];
char *whitespace = " \t\n\r";
char input[80] = "FILELIST.CFG"; /* Default config file */
FILE *outfile;
main(int argc, char *argv[])
{
FILE *fp;
char tempbuff[100];
int t;
numbytes = areabytes = 0;
window(1,1,80,25);
for(t=0; t < 80; t++)
{
if((path[t] = malloc(sizeof(struct xx))) == NULL)
{
puts("not enough memory! aborting...");
exit(255);
}
}
puttext(1,1,80,25,IMAGEDATA); /* Throw up our screen */
textbackground(BLUE);
textcolor(LIGHTCYAN);
if(argc > 1) /* assume that argv[1] is a valid config file */
{
strcpy(input, argv[1]);
gotoxy(14,23);
textcolor(YELLOW);
cprintf("Using configuration file %s",input);
textcolor(LIGHTCYAN);
}
parsectl(); /* parse the control file */
/* Normalize the first two paths for reading and writing */
fnsplit(path[0]->FilePath, drive, dir, filename, ext);
fnmerge(path[0]->FilePath, drive, dir, filename, ext);
fnsplit(path[1]->FilePath, drive, dir, filename, ext);
fnmerge(path[1]->FilePath, drive, dir, filename, ext);
if((outfile = fopen(path[0]->FilePath, "wt")) == NULL) /* open output file */
{
gotoxy(14,22);
cprintf("Error opening %s ",path[0]->FilePath);
exit(255);
}
if((fp = fopen(path[1]->FilePath, "rt")) == NULL) /* open header file */
{
gotoxy(14,22);
cprintf("Error opening %s ",path[1]->FilePath);
exit(255);
}
while(fgets(tempbuff,100,fp) != NULL) /* write header file line by line */
fputs(tempbuff,outfile);
fclose(fp); /* Close header file */
listfiles(); /* process each FILES.BBS */
for(t=0;t<80;t++) free(path[t]); /* free the memory */
fclose(outfile); /* close the output file */
gotoxy(1,24); /* put cursor at bottom of screen */
return(1); /* all done */
}
/*
** commafmt()
**
** Public domain by Bob Stout
**
** Notes: 1. Use static buffer to eliminate error checks on buffer overflow
** and reduce code size.
** 2. By making the numeric argument a long and prototyping it before
** use, passed numeric arguments will be implicitly cast to longs
** thereby avoiding int overflow.
** 3. Use the thousands grouping and thousands separator from the
** ANSI locale to make this more robust.
*/
/* Buffer for formatted string */
/* Size of buffer */
/* Number to convert */
commafmt(char *buf, int bufsize, long N)
/*size_t commafmt(char *buf, int bufsize, long N)*/
{
int len = 1, posn = 1, sign = 1;
char *ptr = buf + bufsize - 1;
if (2 > bufsize)
{
ABORT: *buf = NUL;
return 0;
}
*ptr-- = NUL;
--bufsize;
if (0L > N)
{
sign = -1;
N = -N;
}
for ( ; len <= bufsize; ++len, ++posn)
{
*ptr-- = (char)((N % 10L) + '0');
if (0L == (N /= 10L))
break;
if (0 == (posn % 3))
{
*ptr-- = ',';
++len;
}
if (len >= bufsize) goto ABORT;
}
if (0 > sign)
{
if (0 == bufsize) goto ABORT;
*ptr-- = '-';
}
strcpy(buf, ++ptr);
/*return (size_t)len;*/
}
int listfiles()
{
FILE *fp;
static int i, j, count, test = 0, test2 = 0;
char desc[200];
char buf[20];
gotoxy(27,5);
cprintf("%d",(start - 2));
gotoxy(3,8);
textcolor(LIGHTGREEN);
cprintf("Writing Output to :");
textcolor(LIGHTCYAN);
gotoxy(27,8);
cprintf("%s",path[0]->FilePath);
for(i=2;i<start;i++)
{
test = strlen(path[i]->FilePath);
gotoxy(27,4);
cprintf("%d %s",(i-1),path[i]->FilePath);
strcpy(keeppath,path[i]->FilePath);
strcat(path[i]->FilePath,"files.bbs");
if((fp = fopen(path[i]->FilePath,"rt")) == NULL)
{
gotoxy(14,22);
cprintf("Error: Can't find file %s ",path[i]->FilePath);
continue;
}
fprintf(outfile,"\n\n-=- Area: %s\n",path[i]->AreaDesc);
test2 = strlen(path[i]->AreaDesc);
gotoxy(3,10);
textcolor(LIGHTRED);
cprintf("%s",path[i]->AreaDesc);
textcolor(LIGHTCYAN);
gotoxy(1,1);
count = areabytes = 0;
while(fgets(desc,200,fp) != NULL)
if(processline(desc)) count++;
numfiles[i] = count;
tempcount += count;
if(i < 10)
{
gotoxy((test + 29), 4);
for(j = 0; j < (test + 2); j++)
cprintf("\b \b");
}
else
{
gotoxy((test + 30), 4);
for(j = 0; j < (test + 3); j++)
cprintf("\b \b");
}
gotoxy((test2 + 3), 10);
for(j=0;j<test2; j++) cprintf("\b \b");
if(count)
{
commafmt(buf,20,areabytes);
fprintf(outfile,"\n=-= Area Stats: %s Bytes in %d Files",buf,count);
fprintf(outfile,"\n************************************************************************");
gotoxy(27,7);
textcolor(WHITE);
cprintf("%d",tempcount);
commafmt(buf,20,numbytes);
gotoxy(27,6);
textcolor(LIGHTMAGENTA);
cprintf("%s",buf);
textcolor(LIGHTCYAN);
}
else
{
fprintf(outfile,"\n=-= Area Stats: NO FILES");
fprintf(outfile,"\n************************************************************************");
}
fclose(fp);
}
count = 0;
gotoxy(14,22);
cprintf("Computing Statistics... ");
fprintf(outfile,"\n=-=-=-=-=-=-=\nFinal Report:\n=-=-=-=-=-=-=\n");
for(i=2;i<start;i++)
{
fprintf(outfile," %3d Files in%s",numfiles[i],path[i]->AreaDesc);
count += numfiles[i];
}
commafmt(buf,20,numbytes);
gotoxy(27,7);
textcolor(WHITE);
cprintf("%d", count);
gotoxy(27,6);
textcolor(LIGHTMAGENTA);
cprintf("%s",buf);
textcolor(LIGHTCYAN);
fprintf(outfile," ----\n %4d Total Files in %s bytes",count, buf);
fprintf(outfile,"\n* Produced automatically by FILELIST (1991) Erik Vanriper. (1:260/230)\n");
gotoxy(27,4);
cprintf("Done!");
gotoxy(14,22);
cprintf("Thank you for using FILELIST. Pass it on to your friends! ");
return(1);
}
void killspace(char buffer[200])
{
int test;
test = strspn(buffer,whitespace);
strcpy(descrip,(buffer + test));
}
int processline(char desc[200])
{
char filename[13], *test, check[80];
int result, c, i, d;
if((desc[0] < '\x30') || (desc[0] > '\x7A') || ((desc[0] > '\x3A') && (desc[0] < '\x41')) || ((desc[0] > '\x5A') && (desc[0] < '\x61')))
{
fputs(desc,outfile);
return(0);
}
test = strstr(desc, "\x20");
strcpy(descrip,test);
result = strlen(descrip);
i = strlen(desc);
strncpy(filename,desc,(i - result));
filename[(i-result)] = '\0';
strcpy(check,keeppath);
strcat(check,"\\");
strcat(check,filename);
killspace(descrip);
if(stat(check, &info) !=0) return(0);
numbytes += info.st_size;
areabytes += info.st_size;
result = strlen(filename);
fprintf(outfile,"%s",filename);
for(c=0;c<(13-result);c++) fprintf(outfile," ");
fprintf(outfile,"%ld",info.st_size);
tmfile = localtime(&info.st_atime);
if(info.st_size <= 999) fprintf(outfile," %.2d-%.2d-%d ",(tmfile->tm_mon +1),tmfile->tm_mday,tmfile->tm_year);
else if(info.st_size <= 9999) fprintf(outfile," %.2d-%.2d-%d ",(tmfile->tm_mon +1),tmfile->tm_mday,tmfile->tm_year);
else if(info.st_size <= 99999) fprintf(outfile," %.2d-%.2d-%d ",(tmfile->tm_mon +1),tmfile->tm_mday,tmfile->tm_year);
else fprintf(outfile," %.2d-%.2d-%d ",(tmfile->tm_mon +1),tmfile->tm_mday,tmfile->tm_year);
writedescrip();
return(1);
}
writedescrip()
{
char *token;
int test, x = 0;
test = strlen(descrip);
if(test <= 47)
{
fprintf(outfile,"%s",descrip);
return(1);
}
else
{
token = strtok(descrip,whitespace);
while(token != NULL)
{
x = x + strlen(token);
if(x <= 47)
{
fprintf(outfile,"%s ",token);
x = x + 1;
}
else
{
x = 1;
fprintf(outfile,"\n %s ",token);
}
token = strtok(NULL,whitespace);
}
}
fprintf(outfile,"\n");
return(1);
}
int parsectl()
{
FILE *fp;
int result, c, i, d;
char wholeline[200], *test;
if ((fp = fopen(input,"rt")) == NULL)
{
gotoxy(14,22);
cprintf("Can't find file %s ",input);
exit(255);
}
gotoxy(14,22);
cprintf("Parsing configuration file...");
c = 0;
while(fgets(wholeline,200,fp) != NULL)
{
test = strstr(wholeline, "\x20");
if(test == NULL)
{
strcpy(path[c]->FilePath,wholeline);
d = strlen(path[c]->FilePath);
path[c]->FilePath[d] = '\0';
}
else
{
strcpy(path[c]->AreaDesc,test);
result = strlen(wholeline);
i = strlen(path[c]->AreaDesc);
strncpy(path[c]->FilePath,wholeline,(result - i));
path[c]->FilePath[(result - i)] = '\0';
}
c++; start++;
}
fclose(fp);
gotoxy(14,22);
cprintf("Working... ");
return(1);
}